package com.example.demo.controller;


import com.example.demo.service.TestUserImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class HelloController {
    @Resource
    TestUserImpl testUsers;
    @Autowired
    StringRedisTemplate template;

    @RequestMapping("/hello")
    public String hello() {
       // testUser t=testUsers.getUser();
        return "hello,this is a springboot :"+testUsers.getUser().getName();
    }
    @RequestMapping("/setValue")
    public String setValue(){
        if(!template.hasKey("shabao")){
            template.opsForValue().append("shabao", "我是傻宝");
            return "使用redis缓存保存数据成功";
        }else{
            template.delete("shabao");
            return "key已存在";
        }
    }
    @RequestMapping("/getValue")
    public String getValue(){

        if(!template.hasKey("shabao")){
            return "key不存在，请先保存数据";
        }else{
            String shabao = template.opsForValue().get("shabao");//根据key获取缓存中的val
            return "获取到缓存中的数据：shabao="+shabao;
        }
    }


}
